package com.zynn.service.module.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zynn.message.queue.constant.MqQueueNameConstant;
import com.zynn.message.queue.service.ScheduleMessagePostProcessor;
import com.zynn.message.queue.service.SenderService;
import com.zynn.service.module.canal.constant.TableNameConstant;
import com.zynn.service.module.canal.enums.ProfileMnitorColumn;
import com.zynn.service.module.canal.event.InsertCanalEvent;
import com.zynn.service.module.canal.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * @author jerry-liqi
 *
 * canal 监听插入
 */
@Component
@Slf4j
public class InsertCanalListener extends AbstractCanalListener<InsertCanalEvent> {

    @Autowired
    private GroupmembersService groupmembersService;

    @Autowired
    private SenderService senderService;


    /**
     * 串行执行操作
     * @param database 数据库
     * @param table 表名
     * @param rowData 数据
     */
    @Override
    protected void doSync(String database, String table, CanalEntry.RowData rowData) {
        log.info("InsertCanalListener："+ database + table);


        Map<String, Object> columnsMap = super.parseColumnsToMap(rowData.getAfterColumnsList());

        /** 推荐V3 监控推荐**/
        this.recommendFriendMqQueueV3Monitor(table,columnsMap);

        /**
         * 1.数据迁移
         * this.migrateData(table,columnsMap);
         */

        /**
         * 3.私密相册行为推送相关
         * this.groupActionPushMonitor(table,columnsMap);
         */

        /**
         * 4.星光值
         * this.startLightMonitor(table,columnsMap);
         */

        /**
         * 5.个人资料修改时包括生日，城市，职业，学校，学院，年级生成首页动态
         * this.createEventsForModifyProfileInfo(table,columnsMap);
         */

        /**
         * 6.注册动态
         * this.createRegisterEvent(table,columnsMap);
         */
        /**
         * 7.用户A评论B用户的动态1，A关注了C，用户C也评论动态1则用户A也收到通知
         * this.sendCommentMonitor(table,columnsMap);
         */

        /**
         * 点赞活动
         *
         * this.likeActivity(table,columnsMap);
         */
    }


    /**
     * 数据迁移
     * @param table 表名
     * @param columnsMap map列
     */
    private void migrateData(String table,Map<String, Object> columnsMap) {

        if(TableNameConstant.GROUPMEMBERS.equals(table)) {
            groupmembersService.handleInsert(columnsMap);
        }
        if(TableNameConstant.PICTURES.equals(table)) {
            senderService.send(MqQueueNameConstant.MIGRATE_PICTURE_COUNT, columnsMap);
        }
        if(TableNameConstant.GROUPS.equals(table)) {
            senderService.send(MqQueueNameConstant.MIGRATE_GROUP_ON_TIME, columnsMap);
        }
        if(TableNameConstant.SCHOOL_SUBJECT_SHOW_PK.equals(table)){
            senderService.send(MqQueueNameConstant.MIGRATE_SCHOOL_SHOW_PK, columnsMap);
        }

    }

    /**
     * 点赞活动
     * @param table
     * @param columnsMap
     */
    private void likeActivity(String table,Map<String, Object> columnsMap){
        if(TableNameConstant.LIKE.equals(table)) {
            senderService.send(MqQueueNameConstant.LIKE_ACTIVITY_DREAM, columnsMap);
        }
    }


    /**
     * 推荐v3监控
     * @param table 表名
     * @param columnsMap 表的列名
     */
    private void recommendFriendMqQueueV3Monitor(String table,Map<String, Object> columnsMap){

        /** 监控user_friend表 */
        if(TableNameConstant.USER_FRIEND.equals(table)) {
            senderService.send(MqQueueNameConstant.FIND_FRIEND_FORM_USER_FRIEND, columnsMap,new ScheduleMessagePostProcessor(1000L,0L,null,null));
        }

        /** 监控点赞表 */
        if(TableNameConstant.LIKE.equals(table)) {
            senderService.send(MqQueueNameConstant.FIND_FRIEND_FORM_LIKE, columnsMap,new ScheduleMessagePostProcessor(1000L,0L,null,null));
        }

        /** 监控 base_user_info*/
        if(TableNameConstant.BASE_USER_INFO.equals(table)) {
            senderService.send(MqQueueNameConstant.FIND_FRIEND_FORM_BASE_USER_INFO, columnsMap,new ScheduleMessagePostProcessor(1000L,0L,null,null));
        }

        /** 监控艾特表 */
        if(TableNameConstant.REMINDUSER.equals(table)) {
            senderService.send(MqQueueNameConstant.FIND_FRIEND_FORM_REMINDUSER, columnsMap,new ScheduleMessagePostProcessor(1000L,0L,null,null));
        }

        /** 监控评论表 */
        if(TableNameConstant.COMMENTS.equals(table)) {
            senderService.send(MqQueueNameConstant.FIND_FRIEND_FORM_COMMENTS, columnsMap,new ScheduleMessagePostProcessor(1000L,0L,null,null));
        }

        /** 监控关注表 */
        if(TableNameConstant.USER_ATTENTION_RELATION.equals(table)) {
            senderService.send(MqQueueNameConstant.FIND_FIEND_FROM_ATTENTION_RELATION, columnsMap,new ScheduleMessagePostProcessor(1000L,0L,null,null));
        }
    }

    /**
     * 私密相册行为推送表监控
     * @param table
     * @param columnsMap
     */
    private void groupActionPushMonitor(String table,Map<String, Object> columnsMap){
        log.info("-------相册内实时推送canal开始------");
        /**相册内有上传推送**/
        if(TableNameConstant.EVENTS.equals(table)) {
            senderService.send(MqQueueNameConstant.PERSON_GROUP_UPLOAD_PUSH, columnsMap);
        }

        /**被评论推送**/
        if(TableNameConstant.COMMENTS.equals(table)) {
            /**动态被评论**/
            senderService.send(MqQueueNameConstant.BE_COMMENT_PUSH, columnsMap);
            /**评论被回复**/
            senderService.send(MqQueueNameConstant.COMMENT_BE_REPLY_PUSH, columnsMap);
        }

        /**点赞实时推送**/
        if(TableNameConstant.LIKE.equals(table)) {
            log.info("点赞实时推送canal开始----{}",columnsMap);
            senderService.send(MqQueueNameConstant.BE_LIKED_PUSH, columnsMap);
        }



    }

    /**
     * 星光值监控
     * @param table
     * @param columnsMap
     */
    private void startLightMonitor(String table,Map<String, Object> columnsMap){
        /**上传照片动态**/
        if(TableNameConstant.EVENTS.equals(table)) {
            // 判断是否是照片动态
            Integer eMain = Integer.valueOf(String.valueOf(columnsMap.get("eMain")));
            if(0 == eMain){
                senderService.send(MqQueueNameConstant.UPLOAD_PICTURE_STAR_LIGHT, columnsMap);
            }
            senderService.send(MqQueueNameConstant.UPLOAD_PICTURE_IN_PRIVATE_STAR_LIGHT, columnsMap);
        }

        /**新加入活动送奖励**/
        if(TableNameConstant.SCHOOL_POPULARITY_STAR_LIGHT.equals(table)) {
            senderService.send(MqQueueNameConstant.NEW_JOIN_STAR_LIGHT_PRIZE, columnsMap);
        }

    }



    /**
     * 用户评论监控
     * @param table
     * @param columnsMap
     */
    private void sendCommentMonitor(String table,Map<String, Object> columnsMap) {

        /**被评论推送**/
        if (TableNameConstant.COMMENTS.equals(table)) {
            /**"用户A评论B用户的动态1，A关注了C，用户C也评论动态1则用户A也收到通知"**/
            senderService.send(MqQueueNameConstant.COMMENT_A_TO_B, columnsMap);
        }
    }


    /**
     * 注册动态
     * @param table
     * @param columnsMap
     */
    private void createRegisterEvent(String table,Map<String,Object> columnsMap){
        senderService.send(MqQueueNameConstant.USER_RIGISTER_CREATE_EVENT,columnsMap);
    }




    /**
     * 个人资料修改时包括生日，城市，职业，学校，学院，年级生成首页动态
     * @param table
     * @param columnsMap
     */
    private void createEventsForModifyProfileInfo(String table,Map<String,Object> columnsMap){
        /**base_user_info**/
        if(TableNameConstant.BASE_USER_INFO.equals(table)) {
            Map<String,Object> eventColumnMap = generateResultMap(columnsMap,Arrays.asList(ProfileMnitorColumn.BASE_USER_INFO_COLUMNS.getColumns()));
            if(MapUtils.isNotEmpty(eventColumnMap)){
                senderService.send(MqQueueNameConstant.PROFILE_MODIFIED_CREATE_EVENT,eventColumnMap);
            }
        }

        /**users**/
        if(TableNameConstant.USERS.equals(table)) {
            Map<String,Object> eventColumnMap = generateResultMap(columnsMap,Arrays.asList(ProfileMnitorColumn.USERS_COLUMNS.getColumns()));
            if(MapUtils.isNotEmpty(eventColumnMap)){
                senderService.send(MqQueueNameConstant.PROFILE_MODIFIED_CREATE_EVENT,eventColumnMap);
            }
        }
    }

    private Map<String,Object> generateResultMap(Map<String, Object> columnsMap,List<String> columns) {
        Map<String, Object> eventColumnMap = Maps.newHashMap();
        Object columnVal;
        for (String column: Optional.ofNullable(columns).orElse(Lists.newArrayList())) {
            columnVal = columnsMap.get(column);
            if(Objects.nonNull(columnVal)){
                if(StringUtils.equals(column,"ucity")){
                    columnVal = String.valueOf(columnsMap.get("uprovince"))+"省"+columnVal + "市";
                }
                eventColumnMap.put(column,columnVal);
            }
        }
        return eventColumnMap;
    }


}
